package com.service.impl;

import com.dao.FoodsDao;
import com.dao.OrderDao;
import com.dao.impl.FoodsDaoImpl;
import com.dao.impl.OrderDaoImpl;
import com.entity.Orders;
import com.service.OrderService;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    OrderDao orderDao = new OrderDaoImpl();

    /**
     * 根据id查询所有订单信息
     * @param id
     * @return
     */
    @Override
    public List<Orders> listALLOrder(int id) {
        List<Orders> orders = orderDao.listOrderById(id);
//        int sum=0;
//        for (Orders order:orders) {
//            sum+=order.getMoney()*order.getNumber();
//            order.setSubtotal(sum);
//        }

        return orders;
    }

    /**
     * 通过订单号下单付款
     * @param id
     * @return
     */
    @Override
    public Orders OrderPay(int id) {
        FoodsDao foodsDao = new FoodsDaoImpl();
        Orders orders1 = foodsDao.orderById(id);
        Long aLong = orders1.getoNumber();
        List<Orders> orders2 = foodsDao.orderOnNum(aLong);
        Orders orders3 = new Orders();
        int sum=0;
        for (Orders order:orders2) {
            sum+=order.getMoney()*order.getNumber();
            order.setTotal(sum);
            orders3.setTotal(order.getTotal());
            orders3.setoNumber(order.getoNumber());
        }
        return orders3;
    }

    @Override
    public int upDateState(String oNumber) {
        int i = orderDao.upDateState(oNumber);
        return i;
    }

    @Override
    public List<Map<String,Object>> queryOrderDetail(String oNumber) {
        List<Map<String,Object>> foodsDetail=orderDao.queryOrderDetail(oNumber);
        if (foodsDetail!=null){
            return foodsDetail;
        }
        return null;
    }

    @Override
    public List<Orders> queryOrderAll() {
        List<Orders> ordersAll=orderDao.queryOrderAll();
        int countUserId= orderDao.queryOrderAllByUserId();
        if (ordersAll!=null){
            Iterator<Orders> it = ordersAll.iterator();
            while (it.hasNext()){
                if (ordersAll.size()==countUserId){
                    return ordersAll;
                }
                for (int i=0;i<ordersAll.size();i++){
                    if (ordersAll.get(i).getoNumber().equals(ordersAll.get(i + 1).getoNumber())){
                        int money=ordersAll.get(i).getMoney()+ordersAll.get(i+1).getMoney();
                        ordersAll.remove(i+1);
                        ordersAll.get(i).setMoney(money);
                        break;
                    }
                }
            }
            return ordersAll;
        }
        return null;
    }

    @Override
    public Orders centerPay(String oNumber) {
        FoodsDao foodsDao = new FoodsDaoImpl();
        long aLong = Long.parseLong(oNumber);
        List<Orders> orders2 = foodsDao.orderOnNum(aLong);
        Orders orders3 = new Orders();
        int sum=0;
        for (Orders order:orders2) {
            sum+=order.getMoney()*order.getNumber();
            order.setTotal(sum);
            orders3.setTotal(order.getTotal());
            orders3.setoNumber(order.getoNumber());
        }
        System.out.println(orders3);
        return orders3;
    }

    public static void main(String[] args) {
        OrderService orderService=new OrderServiceImpl();
        System.out.println(orderService.queryOrderDetail("20221212035705"));
    }
}
